package com.example.finaltool.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.finaltool.annotations.Timer;
import com.example.finaltool.consts.AreaCode;
import com.example.finaltool.consts.Table;
import com.example.finaltool.mapper.SysAreaMapper;
import com.example.finaltool.model.AreaInfo;
import com.example.finaltool.model.config.Data;
import com.example.finaltool.model.db.SysArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 同时适用于村和镇
 */
@Service
public class SysAreaService {
    SysAreaMapper mapper;

    @Autowired
    public SysAreaService(
            SysAreaMapper mapper) {
        this.mapper = mapper;
    }

    @Timer(Table.SYS_AREA)
    @Transactional(rollbackFor = Exception.class)
    public AreaInfo save(Data data) {
        AreaInfo info = new AreaInfo();

        String shi = data.getShi();
        Long shiId = shiIdOf(shi);
        info.setShi(shi);
        info.setShiId(shiId);

        String quxian = data.getQuxian();
        SysArea quArea = getQuxian(quxian, shiId);
        if (quArea == null) {
            saveQuxian(shi, quxian, shiId);
            quArea = getQuxian(quxian, shiId);
        }
        assert quArea != null;
        info.setQuId(quArea.getId());
        info.setQu(quxian);

        String zhen = data.getZhen();
        SysArea zhenArea = getZhen(zhen, quArea.getId());
        if (zhenArea == null) {
            saveZhen(zhen, quArea.getId());
            zhenArea = getZhen(zhen, quArea.getId());
        }
        assert zhenArea != null;
        info.setZhenId(zhenArea.getId());
        info.setZhen(zhen);
        return info;
    }

    public static Long shiIdOf(String shi) {
        switch (shi) {
            case "沈阳":
            case "沈阳市":
                return 2L;
            case "铁岭":
            case "铁岭市":
                return 10L;
            case "大连":
            case "大连市":
                return 6L;
            case "鞍山":
            case "鞍山市":
                return 13L;
            case "抚顺":
            case "抚顺市":
                return 159L;
            case "本溪":
            case "本溪市":
                return 112L;
            case "丹东":
            case "丹东市":
                return 12L;
            case "锦州":
            case "锦州市":
                return 7L;
            case "营口":
            case "营口市":
                return 8L;
            case "阜新":
            case "阜新市":
                return 46L;
            case "辽阳":
            case "辽阳市":
                return 9L;
            case "朝阳":
            case "朝阳市":
                return 11L;
            case "盘锦":
            case "盘锦市":
                return 99L;
            case "葫芦岛":
            case "葫芦岛市":
                return 152L;
            default:
                return 0L;
        }
    }

    public SysArea getQuxian(String quXian, Long pid) {
        return like(quXian, pid);
    }

    public SysArea getZhen(String zhen, Long pid) {
        return like(zhen, pid);
    }

    private SysArea like(String name, Long pid) {
        QueryWrapper<SysArea> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", pid).like("name", name);
        List<SysArea> areas = mapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(areas)) {
            return areas.get(0);
        } else {
            return null;
        }
    }

    public void saveQuxian(String shi, String quxian, Long pid) {
        SysArea area = new SysArea();
        area.setLevel("3");
        area.setName(quxian);
        area.setPid(pid);
        area.setAreaCode(AreaCode.get(shi, quxian));
        mapper.insert(area);
    }

    public void saveZhen(String zhen, Long pid) {
        SysArea area = new SysArea();
        area.setLevel("4");
        area.setName(zhen);
        area.setPid(pid);
        mapper.insert(area);
    }
}
